package blog.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringUtil implements ApplicationContextAware {
	private static final Logger log = LoggerFactory.getLogger(SpringUtil.class);

	private static ApplicationContext applicationContext;
	private static ClassPathXmlApplicationContext beanFactory;

	public static Object getBean(String name) {
		log.debug(name);
		try {
			return applicationContext.getBean(name);
		} catch (Exception e) {
			return getBeanFactory().getBean(name);
		}
		// return getBeanFactory().getBean(name);
	}

	public static ClassPathXmlApplicationContext getBeanFactory() {
		if (beanFactory == null) {
			beanFactory = new ClassPathXmlApplicationContext("/spring/*.xml");
		}
		return beanFactory;
	}

	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		applicationContext = arg0;
	}
}
